package com.bdqn.controller;

import com.bdqn.service.RightService;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import java.io.IOException;
import java.io.PrintWriter;

/**
 * 权限控制器
 *
 * @author LILIBO
 * @since 2024-01-21
 */
@Controller
@RequestMapping("/right")
public class RightController {

    @Resource
    private RightService rightService;

    /**
     * 权限刷新
     */
    @RequestMapping("/reload")
    public String reload(HttpServletRequest request, HttpServletResponse response) {
        boolean reloadFlag = rightService.reloadFilterChainDefinitions(); // 重新加载权限
        response.setContentType("text/html;charset=UTF-8");
        try {
            PrintWriter out = response.getWriter();
            out.print("<script>alert('权限刷新" + (reloadFlag ? "成功" : "失败") + "！');location.href='" + request.getContextPath() + "/main';</script>");
            out.flush();
            out.close();
        } catch (IOException e) {
            e.fillInStackTrace();
        }
        return null;
    }

}
